package com.canyou;

import com.canyou.controller.AreaController;
import com.canyou.controller.DefaultController;
import com.canyou.controller.EmployeeController;
import com.canyou.controller.LoginController;
import com.canyou.model.Area;
import com.canyou.model.Employee;
import com.canyou.model.Param;
import com.canyou.model.Role;
import com.canyou.model.User;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

/**
 * API引导式配置
 */
public class CanYouConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("config.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.JSP);
		me.setUploadedFileSaveDirectory(PathKit.getWebRootPath()
				+ getProperty("uploadPath", "/upload/"));
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", LoginController.class);
		me.add("/default", DefaultController.class);
		me.add("/employee", EmployeeController.class);
		me.add("/area", AreaController.class);
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置Druid数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(
				getProperty("jdbcUrl").trim(), getProperty("user").trim(),
				getProperty("password").trim(), getProperty("driver"));
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setDialect(new AnsiSqlDialect());
		arp.setShowSql(true);// 输出sql
		me.add(arp);

		// 配置表和实体映射
		arp.addMapping("Admin_User_tb", "Id", User.class);
		arp.addMapping("Business_Employee_tb", "Id", Employee.class);
		arp.addMapping("Admin_Role_tb", "Id", Role.class);
		arp.addMapping("Public_Param_tb", "Id", Param.class);
		arp.addMapping("Public_AreaDatum_tb", "Id", Area.class);
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new CanYouInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
	}

	/**
	 * 配置全局变量
	 */
	public void afterJFinalStart() {
		JFinal.me().getServletContext()
				.setAttribute("resource", "http://192.168.10.12:121");
	}

	/**
	 * <p>
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * </p>
	 * <p>
	 * 运行此 main方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 * </p>
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 82, "/", 5);
	}
}
